package com.fishingwithme.domain.spots;

import com.fishingwithme.infrastructure.CodeEnum;
import com.fishingwithme.infrastructure.utils.CommonUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.Getter;
import org.json.JSONException;

import java.util.HashMap;
import java.util.Map;

/**
 * 鱼类
 */
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
@Getter
public enum FishType implements CodeEnum {
    JIYU(1, "鲫鱼"),
    LIYU(2, "鲤鱼"),
    LUOFEI(3, "罗非"),
    CAOYU(4, "草鱼"),
    LINGYU(5, "鲮鱼"),
    LIANYONG(6, "鲢鳙"),
    HONGWEI(7, "红尾"),
    HUANGWEI(8, "黄尾"),
    QINGYU(9, "青鱼"),
    HEIYU(10, "黑鱼"),
    JUNYU(11, "军鱼"),
    NIANYU(12, "鲶鱼"),
    YALUO(13, "雅罗"),
    QIAOZUI(14, "翘嘴"),
    QINGSHAO(15, "青梢"),
    LUYU(16, "鲈鱼"),
    GUIYU(17, "桂鱼"),
    HUANGLADING(18, "蓝刀"),
    CIYANZUN(19, "刺眼"),
    MAKOU(20, "马口"),
    BAITIAO(21, "白条"),
    LANDAO(22, "黄辣丁");

    @JsonProperty("type")
    private int type;
    @JsonProperty("name")
    private String name;

    FishType(int type, String name) {
        this.name = name;
        this.type = type;
    }

    private static final Map<Integer, FishType> TYPE_MAP = new HashMap<>();

    static {
        for (FishType type : values()) {
            TYPE_MAP.put(type.getType(), type);
        }
    }

    // 优化后的查找方法（O(1) 时间复杂度）
    public static FishType of(int type) {
        return TYPE_MAP.getOrDefault(type, null);
    }

    /**
     * 返回JSON对象，便于序列化
     */
    public ObjectNode toJson() throws JSONException {
        ObjectNode json = CommonUtils.objectMapper.createObjectNode();
        json.put("code", this.name());
        json.put("type", this.type);
        json.put("name", this.name);
        return json;
    }



    /**
     * 可选：简化的toString()，用于非JSON场景
     */
    @Override
    public String toString() {
        try {
            return this.toJson().toString();
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }
}
